﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;

namespace WareHose.Application.Permissions
{
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v1")]
    public class PermissionService : IDynamicWebApi
    {
        public async Task<ResultDto<string>> GetRole()
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://10.31.52.43:5230");

            //拿token  http://10.31.52.43:5230/api/Web/Login
            var result = await
                client.PostAsJsonAsync("/api/Web/Login", new { userName = "药老", password = "123", AutoCode = string.Empty });

            var token = JsonConvert.DeserializeObject<TokenDto>(await result.Content.ReadAsStringAsync()).token;



            string apiUrl = "/api/Role/RolePage";
            /*string result = await client.GetStringAsync(apiUrl);
            RoleDto role = JsonConvert.DeserializeObject<RoleDto>(result);*/

            client.DefaultRequestHeaders.Add("Authorization", $"bearer {token}");

            var role = await client.GetFromJsonAsync<RoleDto>(apiUrl);

            return new ResultDto<string>
            {
                Code = Status.Success
            };
        }
    }


    public class TokenDto
    {
        public int core { get; set; }
        public string mge { get; set; }
        public string token { get; set; }
    }



    public class RoleDto
    {
        public Datum[] data { get; set; }
        public int totalcount { get; set; }
        public int core { get; set; }
        public object mge { get; set; }
        public object token { get; set; }
    }

    public class Datum
    {
        public long roleId { get; set; }
        public string roleName { get; set; }
        public object roleCode { get; set; }
        public string remark { get; set; }
        public string createName { get; set; }
        public DateTime createTime { get; set; }
    }

}
